package com.hangxintong.controller;

import com.hangxintong.common.AliyunConfig;
import com.hangxintong.common.MailSenderUtils;
import com.hangxintong.common.StatusCode;
import com.hangxintong.common.base.service.UserService;
import com.hangxintong.common.exception.GlobalExceptionHandler;
import com.hangxintong.common.exception.SystemErrorCodeMark;
import com.hangxintong.common.exception.SystemException;
import com.hangxintong.common.utils.RedisUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import io.swagger.annotations.ApiParam;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import com.hangxintong.common.utils.Result;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@CrossOrigin(origins = "*", maxAge = 3600)
@RestController
@Api(tags = {"阿里sdk"})
@RequestMapping(value = "/aliyun")
@Slf4j
public class AliyunController {
    @Resource
    private RedisUtil redisUtil;

    @Resource
    private MailSenderUtils mailSenderUtils;
    @Resource
    private UserService userService;
    /**
    * @Description 发送短信接口
    * @Author  Bz.Mr
    * @Date   2020/12/23 13:59
    *
    */
    @ApiOperation(value = "短信发送验证码", notes = "填写手机号",response = ResponseEntity.class,httpMethod = "GET")
    @GetMapping(value="sendSms/{phone}")
    public Result SendSms(@PathVariable(value = "phone",required = true) String phone,
                          @RequestParam(value = "forget",defaultValue = "0") Integer forget,
                          @RequestParam(value = "status",defaultValue = "0") Integer status,
                          HttpServletRequest request){
        if(!Result.validatePhone(phone)){
            return new Result(false, StatusCode.ERROR,"请正确输入手机号！");
        }
        int num = userService.queryByMobile(phone);
        if(forget==0&&num>0){
            return new Result(false,StatusCode.ERROR,"该用户已注册，请更换手机号！");
        }
        //说明不是注册
        if(forget!=0&&num==0){
            return new Result(false,StatusCode.ERROR,"没有该用户，请更换手机号！");
        }


        //6位序列号
        String msgCode = AliyunConfig.getMsgCode();
        try{
            boolean setRes = redisUtil.set(phone, msgCode);
            Result result=null;
            if(setRes){

                switch (status){
                    case 1:
                        //新用户注册
                        result = AliyunConfig.sendSms(phone, msgCode);

                        break;
                    case 2:
                        //短信验证码登录
                        result = AliyunConfig.sendSmsTextNew(phone,"SMS_148710479", msgCode);

                        break;
                    case 3:
                        //修改密码
                        result = AliyunConfig.sendSmsTextNew(phone,"SMS_148592855", msgCode);

                        break;
                    case 4:
                        //登陆异常验证码
                        result = AliyunConfig.sendSmsTextNew(phone,"SMS_148710478", "");
                        break;
                    case 5:
                        //身份验证登陆码
                        result = AliyunConfig.sendSmsTextNew(phone,"SMS_148710480", msgCode);

                        break;
                    default:
                        result = AliyunConfig.sendSmsTextNew(phone,"SMS_148710480", msgCode);

                        break;
                }
            }
            return result;
        }catch (Exception e){
            throw new SystemException(SystemErrorCodeMark.Code.DATA_NOT_EXIST,"服务器异常！");
        }
    }

    /**
    * @Description
    * @Author  Bz.Mr
    * @Date   2020/12/18 17:45
    * 发送邮箱验证码
    */
    @ApiOperation(value = "邮件验证码发送", notes = "邮件验证码发送",response = ResponseEntity.class,httpMethod = "POST"
    )
    @PostMapping(value="SendMailCode")
    public Result SendMailCode(@RequestParam(value = "email") String email,
                           HttpServletRequest request){
        if(!Result.checkEmail(email)){
            return new Result(false,StatusCode.ERROR,"邮箱格式不正确！");
        }
        //8位序列号
        String msgCode = MailSenderUtils.getMsgCode();
        try{
            System.out.println("=============1>"+msgCode);
            mailSenderUtils.sendSimpleMail(email,"【行信通】","验证码："+msgCode);
            System.out.println("=============2>"+msgCode);

            boolean setRes = redisUtil.set(email, msgCode);
            return Result.ok(msgCode);
        }catch (Exception e){
            log.info("message:"+e.getMessage());
            throw new SystemException(SystemErrorCodeMark.Code.DATA_NOT_EXIST,e.getMessage());
        }
    }
    /**
    * @Description
    * @Author  Bz.Mr
    * @Date   2021/1/26 10:14
    * 阿里云批量发短信
    */
    @ApiOperation(value = "阿里云批量发短信", notes = "阿里云批量发短信",response = ResponseEntity.class,httpMethod = "POST"
    )
    @PostMapping(value="SendBatchSms")
    public Result SendBatchSms(@RequestParam(value = "PhoneNumberJson") @ApiParam(value = "手机号：['18812354687','136123456677']") List<String> PhoneNumberJson,
                               @RequestParam(value = "SignNameJson") @ApiParam(value = "签名名称：['行信通','行信通']") List<String> SignNameJson,
                               @RequestParam(value = "SignNameJson") @ApiParam(value = "签名名称：['行信通','行信通']") List<String> TemplateParamJson,
                               HttpServletRequest request){

        //8位序列号
        String msgCode = MailSenderUtils.getMsgCode();
        try{
            AliyunConfig.SendBatchSms(PhoneNumberJson,SignNameJson,"SMS_148710480",TemplateParamJson);
            return Result.ok(msgCode);
        }catch (Exception e){
            throw new SystemException(SystemErrorCodeMark.Code.DATA_NOT_EXIST,e.getMessage());
        }
    }
}
